

pub trait StringExentd {
    /// 截取字符
    fn sub_string(&self, start_index: usize, len: usize) -> String;

    /// 截取字符为两段
    fn split_two(&self,  lable: impl Into<String>) -> (String, String);

    /// 以字符分隔为数组
    fn split_arr(&self, lable: impl Into<String>) -> Vec<String>;
}

impl StringExentd for &str {
    /// 截取字符
    fn sub_string(&self, start_index: usize, len: usize) -> String {
        let value = self.to_string();
        let v_len = value.chars().count();

        // 截取数据时, 验证长度够不够截取
        if v_len < start_index + len {
            return String::new();
        }

        let t = value
            .chars()
            .into_iter()
            .map(|x| x.to_string())
            .collect::<Vec<_>>();
        if len > t.len() {
            return "".to_owned();
        }
        let r: String = t[start_index..start_index + len].concat();
        return r;
    }
    /// 截取字符
    fn split_two(&self, lable: impl Into<String>) -> (String, String) {
        let value = self.to_string();
        let lable = lable.into();
        let lable_len = lable.len();
        let value_len = value.len();

        let index = match value.find(&lable) {
            None => return (value, String::new()),
            Some(v) => v,
        };

        let index_start = index;
        let lable_end = index + lable_len;

        let r: String = value[0..index_start].to_string();
        let r2: String = value[lable_end..value_len].to_string();
        (r, r2)
    }

    fn split_arr(&self, lable: impl Into<String>) -> Vec<String> {
        let value = self.to_string();
        let lable = lable.into();
        let mut arr = vec![];
        let (s1, s2) = value.split_two(lable.as_str());
        arr.push(s1);
        if s2.as_str() != "" {
            let mut arr2 = s2.split_arr(lable.as_str());
            arr.append(&mut arr2);
        }
        arr
    }
}

impl StringExentd for String {
    fn sub_string(&self, start_index: usize, len: usize) -> String {
        self.as_str().sub_string(start_index, len)
    }

    fn split_two(&self, lable: impl Into<String>) -> (String, String) {
        self.as_str().split_two(lable)
    }

    fn split_arr(&self, lable: impl Into<String>) -> Vec<String> {
        self.as_str().split_arr(lable)
    }
}

#[test]
fn test_old_sub_string() {
    let s = String::from("测试获得bytes");
    println!("test: {:?}", s.as_bytes()); // abcdef: ("ab", "def")

    let a = "郭宇b欢迎你";
    let v = a.sub_string(2, 1);
    println!("{}: {:?}", a, v); // abcdef: ("ab", "def")

    let a = "郭宇好欢迎你";
    let v = a.sub_string( 2,1);
    println!("{}: {:?}", a, v); // 郭宇abc欢迎你: ("郭", "abc欢迎你")

    let a = "郭宇欢迎你,查找一个找不到的中文字符";
    let v = a.split_two( "好");
    println!("{}: {:?}", a, v); // 郭宇abc欢迎你: ("郭", "abc欢迎你")

    let a = "郭宇欢迎你,查找一个找不到的英文字符呀";
    let v = a.split_two( "e");
    println!("{}: {:?}", a, v); // 郭宇abc欢迎你: ("郭", "abc欢迎你")

    let a = "郭宇欢迎你,ab查找一个英文字符串";
    let v = a.split_two( ",ab");
    println!("{}: {:?}", a, v); // 郭宇abc欢迎你: ("郭", "abc欢迎你")

    let a = "郭宇欢迎你abc,你好查找一个中文字符串";
    let v = a.split_two("你好");
    println!("{}: {:?}", a, v); // 郭宇abc欢迎你: ("郭", "abc欢迎你")

    let a = "郭宇abc欢迎你,查找一个中英混和字符串";
    let v = a.split_two("abc欢迎你,");
    println!("{}: {:?}", a, v); // 郭宇abc欢迎你: ("郭", "abc欢迎你")
}

#[test]
fn test_sub_string() {
    let a = "abcdef";
    let v = a.sub_string(0, 2);
    println!("{}: {}", a, v);

    let a = "郭宇abc欢迎你";
    let v = a.sub_string( 0, 2);
    println!("{}: {}", a, v);

    let v = String::from("郭宇abc欢迎你").sub_string( 0, 3);
    println!("{}: {}", a, v);
}

#[test]
fn test_split_two() {
    let a = "abcd ef";
    let v = a.split_two( "c");
    println!("{}: {:?}", a, v);

    let a = "郭宇 欢迎你"; //
    let v = a.split_two(" ");
    println!("{}: {:?}", a, v);

    let a = "郭宇欢迎你"; //
    let v = a.split_two("欢迎");
    println!("{}: {:?}", a, v);

    let a = "郭宇欢迎你"; //
    let v = a.split_two("欢迎fd");
    println!("{}: {:?}", a, v);

    let a = "郭宇欢迎q你"; //
    let v = a.split_two("欢迎q");
    println!("{}: {:?}", a, v);
}

#[test]
fn test_split_arr() {
    let a = "abcdef";
    let v = a.split_arr( "c");
    println!("{}: {:?}", a, v);

    let a = "郭,宇欢,迎你,"; //
    let v = a.split_arr( ",");
    println!("{}: {:?}", a, v);
}
